package cn.javase.concurrent._设计模式._终止模式之两阶段终止._01_利用isInterrupted;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TPTInterrupt {

    private Thread thread;  // 监控线程

    public void start() {
        thread = new Thread(() -> {
            while (true) {
                Thread current = Thread.currentThread();
                if (current.isInterrupted()) {
                    log.debug("料理后事");
                    break;
                }
                try {
                    Thread.sleep(1000);
                    log.debug("将结果保存");
                } catch (InterruptedException e) {
                    current.interrupt();
                }
            }
        }, "监控线程");
        thread.start();
    }

    public void stop() {
        thread.interrupt();
    }

}
